package com.xinzhi.controller;

import com.xinzhi.service.TransferUserService;
import com.xinzhi.service.impl.BankAccountNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * BalanceController 是一个处理银行账户余额相关请求的 REST 控制器。
 */
@RestController
@RequestMapping("/api/balance")
public class BalanceController {

    @Autowired
    private TransferUserService transferUserService;


    /**
     * 处理获取特定银行账户余额的 HTTP GET 请求。
     *
     * @param bankAccount 要查询余额的银行账户号码
     * @return 包含余额的 ResponseEntity 对象，如果未找到则返回相应的错误状态
     */
    @GetMapping("/{bankAccount}")
    public ResponseEntity<BigDecimal> getBalanceByBankAccount(@PathVariable String bankAccount) {
        try {
            BigDecimal balance = transferUserService.findBalanceByBankAccount(bankAccount);
            return ResponseEntity.ok(balance);
        } catch (BankAccountNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
